package Main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Catalog 
{
	public ArrayList<TheaterEvent> Events;
	
	public Catalog(String fileName)
	{
		Events=new ArrayList<TheaterEvent>();
		LoadEvents(fileName);
	}
	public ArrayList<TheaterEvent> GetAllEvents()
	{
		return new ArrayList<TheaterEvent>(Events);
	}
	private void LoadEvents(String fileName)
	{
		int iLine=0;
		
		try 
		{
			Scanner sc=new Scanner(new FileInputStream(fileName));
			while(sc.hasNext())
			{
				try
				{
					iLine++;
					LoadEvent(sc.nextLine());
				}
				catch (Exception e)
				{
					System.out.println("Catalog: line "+iLine+" in "+fileName+" is corrupted");
				}
			}
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
	}
	private void LoadEvent(String eventLine)
	{
		Scanner sc=new Scanner(eventLine);
		sc.useDelimiter("\\\"");
		String title=sc.next();
		sc.useDelimiter("\\s");
		sc.next();
		String date=sc.next();
		String time=sc.next();
		int hallId=sc.nextInt();
		Events.add(new TheaterEvent(title, date, time, hallId, GetHallCapacity(hallId)));		
	}
	public TheaterEvent GetEvent(String title, String date, String time, int hallId) throws TheaterException
	{
		for(TheaterEvent e : Events)
		{
			if(e.Title.equals(title)
					&&e.Date.equals(date)
					&&e.Time.equals(time)
					&&e.HallId==hallId)
				return e;
		}
		throw new TheaterException("Catalog: An event with such time, date, time and/or place does not exist.");
	}
	//public abstract int GetHallCapacity(int id);
	public int GetHallCapacity(int id)
	{
		int capacity=0;
		switch(id)
		{
			case 1:
				capacity=10;
				break;
			case 2:
				capacity=20;
				break;
			case 3:
				capacity=30;
				break;
			default: 
				break;
		}
		return capacity;
	}
	public String toString()
	{
		String str="Catalogue:\n";
		for(TheaterEvent e : Events)
			str+=e+"\n";
		return str;
		
	}
}
